package cn.csc.module.shoppingCard.controller.admin.project;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shoppingCard.controller.admin.project.vo.*;
import cn.csc.module.shoppingCard.convert.project.CardProjectConvert;
import cn.csc.module.shoppingCard.dal.dataobject.project.CardProjectDO;
import cn.csc.module.shoppingCard.service.project.CardProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 购物卡项目")
@RestController
@RequestMapping("/shoppingCard/card-project")
@Validated
public class CardProjectController {

    @Resource
    private CardProjectService cardProjectService;

    @PostMapping("/create")
    @ApiOperation("创建购物卡项目")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:create')")
    public CommonResult<BigInteger> createCardProject(@Valid @RequestBody CardProjectCreateReqVO createReqVO) {
        return success(cardProjectService.createCardProject(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新购物卡项目")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:update')")
    public CommonResult<Boolean> updateCardProject(@Valid @RequestBody CardProjectUpdateReqVO updateReqVO) {
        cardProjectService.updateCardProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除购物卡项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:delete')")
    public CommonResult<Boolean> deleteCardProject(@RequestParam("id") BigInteger id) {
        cardProjectService.deleteCardProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得购物卡项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:query')")
    public CommonResult<CardProjectRespVO> getCardProject(@RequestParam("id") BigInteger id) {
        CardProjectDO cardProject = cardProjectService.getCardProject(id);
        return success(CardProjectConvert.INSTANCE.convert(cardProject));
    }

    /**
     * 购物卡项目的数据统计
     */
    @GetMapping("/getStatistic")
    @ApiOperation("购物卡项目的数据统计")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:query')")
    public CommonResult<CardProjectStatisticRespVO> getStatistic(@RequestParam("id") BigInteger id) {
        return success(cardProjectService.getStatistic(id));
    }

    @GetMapping("/list")
    @ApiOperation("获得购物卡项目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:query')")
    public CommonResult<List<CardProjectRespVO>> getCardProjectList(@RequestParam("ids") List<BigInteger> ids) {
        List<CardProjectDO> list = cardProjectService.getCardProjectList(ids);
        return success(CardProjectConvert.INSTANCE.convertList(list));
    }

    /**
     * 获得所有购物卡项目列表(只包含ID和name)
     */
    @GetMapping("/listAll")
    @ApiOperation("获得所有购物卡项目列表")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:query')")
    public CommonResult<List<CardProjectRespVO>> listAll() {
        List<CardProjectDO> list = cardProjectService.ListAll();
        return success(CardProjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得购物卡项目分页")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:query')")
    public CommonResult<PageResult<CardProjectRespVO>> getCardProjectPage(@Valid CardProjectPageReqVO pageVO) {
        PageResult<CardProjectDO> pageResult = cardProjectService.getCardProjectPage(pageVO);
        PageResult<CardProjectRespVO> convertPage = CardProjectConvert.INSTANCE.convertPage(pageResult);
        convertPage.getList().forEach(c -> c.setStatus(LocalDateTime.now().isBefore(c.getExpireDate()) ? 1 : 0));
        return success(convertPage);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出购物卡项目 Excel")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-project:export')")
    @OperateLog(type = EXPORT)
    public void exportCardProjectExcel(@Valid CardProjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CardProjectDO> list = cardProjectService.getCardProjectList(exportReqVO);
        // 导出 Excel
        List<CardProjectExcelVO> datas = CardProjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "购物卡项目.xls", "数据", CardProjectExcelVO.class, datas);
    }

}
